package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("checkItem")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;

    @RequestMapping("add")
    public Result add(@RequestBody CheckItem checkItem) {
        return checkItemService.add(checkItem);
    }

    //分页查询
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        return checkItemService.pageQuery(queryPageBean);
    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        return checkItemService.delete(id);
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem) {
        CheckItem data = (CheckItem) findById(checkItem.getId()).getData();
        if (data.equals(checkItem)) {
            return Result.success("数据未修改", null);
        }
        return checkItemService.edit(checkItem);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        CheckItem checkItem = checkItemService.findById(id);
        return Result.success(MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        return checkItemService.findAll();
    }
}
